package com.haima.sage.bigdata.api.frame.datatype.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.haima.sage.bigdata.api.common.Constants;
import com.haima.sage.bigdata.api.frame.datatype.DataTypeService;

public class StringDataTypeService extends DataTypeService<String> {
    private static final Logger LOG = LogManager
            .getLogger("com.socsight.coral.common.service.dataType.bean.StringDataTypeService");
    private SimpleDateFormat sdf;

    private boolean formatDate = true;

    public StringDataTypeService() {
        super();
        try {
            sdf = new SimpleDateFormat(Constants.DATE_PATTERN);
        } catch (Exception e) {
            LOG.error("default date pattern error", Constants.DATE_PATTERN);
        }
    }

    @Override
    public String trans(Object value) {
        if (null == value) {
            return null;
        }
        if (formatDate) {
            if (value instanceof Timestamp) {
                if (sdf == null) {
                    return value.toString();
                }
                return sdf.format(new Date(((Timestamp) value).getTime()));
            } else if (value instanceof Date) {
                if (sdf == null) {
                    return value.toString();
                }
                return sdf.format((Date) value);
            }
        }
        return value.toString();
    }

}
